package org.swxjava.util;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

public class MessageBundle {

	/** Logger */
	private static final Log LOG = LogFactory.getLog(MessageBundle.class);

	/**
	 * Load resource bundle from org/swxjava/message.properties
	 */
	private static final ResourceBundle MESSAGES_RESOURCE_BUNDLE = ResourceBundle
			.getBundle("org.swxjava.messages", Locale.getDefault());

	public MessageBundle() {
	}

	public static String getString(String key) {
		return getString(key, null);
	}

	public static String getString(String key, Object arg0) {
		return getString(key, new Object[] { arg0 });
	}

	public static String getString(String key, Object arg0, Object arg1) {
		return getString(key, new Object[] { arg0, arg1 });
	}

	public static String getString(String key, Object arg0, Object arg1,
			Object arg2) {
		return getString(key, new Object[] { arg0, arg1, arg2 });
	}

	public static String getString(String key, Object arg0, Object arg1,
			Object arg2, Object arg3) {
		return getString(key, new Object[] { arg0, arg1, arg2, arg3 });
	}

	public static String getString(String key, Object arg0, Object arg1,
			Object arg2, Object arg3, Object arg4) {
		return getString(key, new Object[] { arg0, arg1, arg2, arg3, arg4 });
	}

	public static String getString(String key, Object[] args) {
		try {
			String message = MESSAGES_RESOURCE_BUNDLE.getString(key);
			if (args == null) {
				return message;
			} else {
				return MessageFormat.format(message, args);
			}
		} catch (MissingResourceException mre) {
			String keyStr = "Could not find the message : " + key
					+ ArrayUtils.toString(args);
			LOG.error(keyStr, mre);
			return "!" + keyStr + "!";
		}
	}
}